package org.hero.service.impl;

import com.google.gson.reflect.TypeToken;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hero.dao.NewsDao;
import org.hero.entity.*;
import org.hero.service.UserService;
import org.hero.util.GsonUtil;
import org.hero.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuzhongyao on 16/8/2.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private NewsDao newsDao;

    public List<News> getNewsList() {
        return newsDao.findAll();
    }

    public List<Hierarchy> findAllHierarchy() {
        return newsDao.findAllHierarchy();
    }

    public List<StationInformation> findAllStationInformation() {
        return newsDao.findAllStationInformation();
    }

    public List<CommunityInformation> findAllCommunityInformation(String suggest) {
        return newsDao.findAllCommunityInformation(suggest);
    }

    public void saveHierarchy(Hierarchy hierarchy) {
        newsDao.saveHierarchy(hierarchy);
    }

    public Integer findAllNum() {
        return newsDao.findAllNum();
    }

    public List<QunSummary> findweek(String suggest) {
        return newsDao.findweek(suggest);
    }

    public QunSummary findSix(String suggest) {
        return newsDao.findSix(suggest);
    }

    public List<UploadData> selectNum(String suggest) {
        return newsDao.selectNum(suggest);
    }

    public List<QunSummary> selectCake(String suggest) {
        return newsDao.selectCake(suggest);
    }

    public List<QunSummary> selectAll() {
        return newsDao.selectAll();
    }

    public List<UploadData> selectZhu(String suggest) {
        return newsDao.selectZhu(suggest);
    }

    public List<Hierarchy> selectName(Map map) {
        return newsDao.selectName(map);
    }

    public Integer selectNum1(String suggest) {
        return newsDao.selectNum1(suggest);
    }

    public Integer selectNum2(String suggest) {
        return newsDao.selectNum2(suggest);
    }

    public boolean impExcel(File file) {
        boolean isok = true;
        //String name = "C:\\Users\\lsd\\Documents\\Tencent Files\\1184107680\\FileRecv/LTE公参.xlsx";
        ArrayList list = new ArrayList();
//        List listn = newsDao.findAllHierarchy();
//        //System.out.println(listn);
        try {
            // 对读取Excel表格内容测试
            InputStream is2 = new FileInputStream(file);
            org.apache.poi.ss.usermodel.Workbook wb = null;
            if (file.getName().endsWith(".xls")) {
                wb = new HSSFWorkbook(is2);
            } else {
                wb = new XSSFWorkbook(is2);//处理以.xlsx结尾的excel
            }
            Sheet sheet = wb.getSheetAt(0);
            //sheet.getRow(0)这是以第一行为主，列数也是第一行的列数
            int rsColumns = sheet.getRow(0).getPhysicalNumberOfCells();
            //主要通过获取最后一行行数并且在此基础上加上1；
            int rsRows = sheet.getLastRowNum() + 1;

            newsDao.delCommunity();
            newsDao.delStation();
            Map<String, GoogleToBaidu> maps = new HashMap<String, GoogleToBaidu>();
            for (int i = 1; i < rsRows; i++) {
                Row row = sheet.getRow(i);//获取第i行
                int j;
                //区别3：判断单元格是否为空的方法不同
                if (row.getCell(1) != null) {
                    ArrayList list1 = new ArrayList();
                    for (j = 0; j < rsColumns; j++) {
                        Cell cell = row.getCell(j);
                        //区别4：在jxl中我们取单元格中的元数的时候全部当成是字符串进行取，f
                        // 但poi中单元格是区分类型的，比如此处是获取字符串类型的元素
                        list1.add(cell);
                        //区别4：此处获取数字类型的元素

                    }
                    StationInformation stationInformation = new StationInformation();
                    CommunityInformation communityInformation = new CommunityInformation();
                    //Integer num2 = newsDao.findAllNumStationInformation()+1;
                    //Integer num3 = newsDao.findAllNumCommunityInformation()+1;

                    String str0 = String.valueOf(list1.get(0));
                    String str1 = String.valueOf(list1.get(1));
                    Integer str2 = (int) Double.parseDouble(String.valueOf(list1.get(2)));
                    Integer str3 = (int) Double.parseDouble(String.valueOf(list1.get(3)));

                    Float str4 = 0.0f;
                    Float str5 = 0.0f;


                    String key = String.valueOf(list1.get(4)) + "," + String.valueOf(list1.get(5));//判断map中是否存在已转换的坐标
                    if (maps.get(key) == null) {
                        String res = HttpUtil.getURLContent("http://api.map.baidu.com/geoconv/v1/?coords=" + key + "&from=3&to=5&ak=rR137ElWogTtA8I7MRD1eumladqqg3GO");
                        GoogleToBaidu gtb = GsonUtil.getIstance().fromJson(res, new TypeToken<GoogleToBaidu>() {
                        }.getType());
                        try {
                            str4 = gtb.getResult().get(0).getX();
                            str5 = gtb.getResult().get(0).getY();
                            System.out.println("转换前:" + key + "->转换后:" + str5 + "," + str4);
                        } catch (Exception e) {
                            System.out.println("这条数据没有坐标");
                        }
                        maps.put(key, gtb);
                    } else {
                        GoogleToBaidu gtb = maps.get(key);
                        try {
                            str4 = gtb.getResult().get(0).getX();
                            str5 = gtb.getResult().get(0).getY();
                            System.out.println("转换前:" + key + "->转换后:" + str5 + "," + str4 + "------ 凯吃");
                        } catch (Exception e) {
                            System.out.println("这条数据没有坐标");
                        }

                    }
                    //Float str4  =  Float.valueOf(String.valueOf(list1.get(4)));
                    //Float str5  =  Float.valueOf(String.valueOf(list1.get(5)));
                    Integer str6 = 0;
                    try{
                        str6 = (int) Double.parseDouble(String.valueOf(list1.get(6)));
                    }catch (Exception e){

                    }

                    //communityInformation.setId(num3);
                    communityInformation.setAreaName(str0);
                    communityInformation.setCellName(str1);
                    communityInformation.setENodeBID(str2);
                    communityInformation.setCellID(str3);
                    communityInformation.setLatitude(str4);
                    communityInformation.setLongitude(str5);
                    communityInformation.setPCI(1);
                    communityInformation.setAzimuth(str6);
                    newsDao.saveCommunityInformation(communityInformation);
                    Integer pp = newsDao.findOnlyNumStationInformation(String.valueOf(str2));
                    if (pp == 0) {
                        //stationInformation.setId(num2);
                        stationInformation.setENodeBID(str2);
                        stationInformation.setLatitude(str4);
                        stationInformation.setLongitude(str5);
                        stationInformation.setAzimuth(str6);
                        stationInformation.setSupplier("");
                        stationInformation.setAreaName(str0);
                        newsDao.saveStationInformation(stationInformation);
                    }
                    //System.out.println(list1);
                    list.add(list1);
                } else {
                    continue;
                }
            }
        } catch (FileNotFoundException e) {
            //System.out.println("未找到指定路径的文件!");
            e.printStackTrace();
            isok = false;
        } catch (IOException e) {
            e.printStackTrace();
            isok = false;
        }

        return isok;
    }


}
